<?php
/**
 * Created by PhpStorm.
 * User: jacob
 * Date: 18-10-16
 * Time: 上午9:56
 */
class InitDir{
  const DIRLIST = [
    'api',
    "common",
    "db",
    "event",
    "cli"
  ];

  const CONFIG = [
    'Config'
  ];

  const FILEEXT = [
    ".inc"
  ];
}

class PhpInit{

  /**
   * @var int
   * 1为已经加载
   * 2为未加载
   */
  private static $status;
  /**
   * @var array
   * 初始化加载类文件
   */
  private static $classFile = [];


  /**
   * 初始化加载文件
   */
  public static function init(){
    $path_arr = [];
    $path = dirname(__FILE__);
    foreach (InitDir::DIRLIST as $item){
      self::getDirList($path."/".$item,$path_arr);
    }
    foreach ($path_arr as $item){
      if (!in_array($item,self::$classFile)){
        self::$classFile[] = $item;
        require_once $item;
      }
    }
  }

  public static function loadClass(){
    if (self::$status != 2){
      foreach (self::$classFile as $item){
        require_once $item;
      }
      foreach (InitDir::CONFIG as $item){
        require_once $item.InitDir::FILEEXT[0];
      }
      self::$status = 2;
    }
  }

  private static function getDirList(string $path,array &$path_arr){
    $list = scandir($path);
    foreach ($list as $item){
      if (is_dir($path."/".$item)){
        self::getDirList($path."/".$item,$path_arr);
        continue;
      }
      $path_arr[] = $path."/".$item;
    }
  }


}